﻿using MediatR;
using SystemHYS.API.Application.command.RawmaterialtestingCommand;
using SystemHYS.Domian.DTO;
using SystemHYS.Infrstructre.Interface;
using SystemHYS.Infrstructre;
using Newtonsoft.Json;
using Share;
using static Share.ParseAPI;
using SystemHYS.Domian;
namespace SystemHYS.API.Application.commandler.RawmaterialtestingCommandler
{
    public class RawmaterialtestingModelCommandler : IRequestHandler<RawmaterialtestingModelCommand, RawmaterialtestingDTO>
    {
        IRawmaterialtestingGualityProjectRepository rawmaterialtestingGualityProjectRepository;
        IGualityProjectRepository gualityProjectRepository;
        IRawmaterialtestingRepository rawmaterialtestingRepository;
        public SystemCoretext db;
        public RawmaterialtestingModelCommandler(IRawmaterialtestingGualityProjectRepository rawmaterialtestingGualityProjectRepository, IRawmaterialtestingRepository rawmaterialtestingRepository, SystemCoretext db, IGualityProjectRepository gualityProjectRepository)
        {
            this.rawmaterialtestingGualityProjectRepository = rawmaterialtestingGualityProjectRepository;
            this.rawmaterialtestingRepository = rawmaterialtestingRepository;
            this.db = db;
            this.gualityProjectRepository=gualityProjectRepository;
        }
        public async Task<RawmaterialtestingDTO> Handle(RawmaterialtestingModelCommand request, CancellationToken cancellationToken)
        {
            var list = from a in await gualityProjectRepository.GetAllAsnc() select a;
            var raw = from a in await rawmaterialtestingGualityProjectRepository.GetAllAsnc() select a;
            var plist=(from a in await rawmaterialtestingRepository.GetAllAsnc() where  a.RawMaterialtestingId==request.RawMaterialtestingId select new RawmaterialtestingDTO
            {
                RawMaterialtestingId=a.RawMaterialtestingId,
                qualityNumber = a.qualityNumber,
                qualityName = a.qualityName,
                qualityDate = a.qualityDate,
                qualityPerson = a.qualityPerson,
                vendor = a.vendor,
                ingredientNumber = a.ingredientNumber,
                ingredientName = a.ingredientName,
                unit = a.unit,
                specification = a.specification,
                rawMaterialBatchNumber = a.rawMaterialBatchNumber,
                size = a.size,
                typeOfQC = a.typeOfQC,
                gualitySchemeName = a.gualitySchemeName,
                state = a.state,
                GualityProjectList = (from b in  raw 
                                join c in list on b.gualityProjectId equals c.gualityProjectId select c).ToList()

            }).FirstOrDefault();
            return plist;
        }
        public async Task<List<RawMaterial>> GetFinishedProduct(int pageindex = 1, int pagesize = 1000)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIzMyIsInNpZCI6IjE4NjcwOTg1NDU4NTM2OTgwNDgiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiIiwianRpIjoiMTYzZWM0ZTYtMTM0Yi00NmYwLWIxYmEtMDFhNjkwNWU0OWFjIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6Iui0uuiJs-W1qSIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL3VyaSI6Ii8iLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.xqb5lfaYGcOKr7lXwZY5ZnAN8gzcqYfc1z9dUKieVfI";
            HttpClientWrapper cilent = new HttpClientWrapper(token);
            string chanpin = "http://47.96.140.177:8000/RawApi/api/RawMaterials/RawMaterialShow?Pageindex=" + pageindex + "&Pagesize=" + pagesize;
            string postreonse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<RawMaterial>>(postreonse);
            return json.data.plist;
        }
    }
}
